package com.mondo.financialwallet.presentation.utils;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.StyleRes;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.mondo.financialwallet.R;

import java.io.File;

public class AndroidUtils {

    // 复制到剪切板
    public static void copyToChipBoard(Context context, String content) {
        ClipboardManager cm = (ClipboardManager) context.getApplicationContext()
                .getSystemService(Context.CLIPBOARD_SERVICE);
        assert cm != null;
        cm.setPrimaryClip(ClipData.newPlainText(null, content));
    }

    // 光标移动到最后
    public static void setTextAndSelectionEnd(TextView textView, EditText editText) {
        setTextAndSelectionEnd(textView.getText().toString(), editText);
    }

    // 光标移动到最后
    public static void setTextAndSelectionEnd(EditText editText) {
        setTextAndSelectionEnd(editText.getText().toString(), editText);
    }

    // 光标移动到最后
    public static void setTextAndSelectionEnd(String string, EditText editText) {
        if (!TextUtils.isEmpty(string)) {
            editText.setText(string);
            editText.setSelection(string.length());
        } else {
            editText.setText(string);
        }
    }

    // 内容可见性切换
    public static void contentVisibilityToggle(CheckBox checkBox, EditText editText){
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            editText.setInputType(isChecked
                    ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                    : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            setTextAndSelectionEnd(editText.getText().toString(), editText);
        });
    }

    // URI 转 File
    public static File uriToFile(Activity activity, Uri uri) {
        String[] image = {MediaStore.Images.Media.DATA};
        Cursor cursor = activity.managedQuery(uri, image, null, null, null);
        int actualImageColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String imgPath = cursor.getString(actualImageColumnIndex);
        return new File(imgPath);
    }

    // 按键状态
    public static void toggleButtonState(Button button, boolean enabled){
        if (enabled){
            button.setBackgroundResource(R.drawable.shape_dialog_positive_button);
            button.setEnabled(true);
            button.setTextColor(button.getResources().getColor(android.R.color.white));
        } else {
            button.setBackgroundResource(R.drawable.shape_rect_unfocused_button);
            button.setEnabled(false);
            button.setTextColor(button.getResources().getColor(R.color.colorUnfocusedText));
        }
    }

    public static void toggleLiquidationButtonState(Button button, boolean enabled) {
        if (enabled) {
            button.setBackgroundResource(R.drawable.shape_dialog_negative_button);
            button.setEnabled(true);
            button.setTextColor(button.getResources().getColor(android.R.color.white));
        } else {
            button.setBackgroundResource(R.drawable.shape_rect_unfocused_button);
            button.setEnabled(false);
            button.setTextColor(button.getResources().getColor(R.color.colorUnfocusedText));
        }
    }
}
